/* 
 * Copyright [2018] [Alex/libo(liboware@gmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.infodb.error.advice;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.hyts.infodb.bean.dto.BaseDTO;
import com.hyts.infodb.config.SystemConfiguration;
import com.hyts.infodb.error.ApplicationException;
import com.hyts.infodb.error.subclass.ControllerException;
import com.hyts.infodb.error.subclass.DaoException;
import com.hyts.infodb.error.subclass.ServiceException;
import com.hyts.infodb.error.subclass.WebException;

import lombok.extern.slf4j.Slf4j;

/** 
 * @author LiBo/Alex
 * @see com.hyts.infodb.error.ApplicationException
 * @since TODO引入特定改变发布版本
 * @version V1.0 
 */
@RestControllerAdvice
@Slf4j
public class SystemExceptionHandler {

    /**  
     * @fieldName DEFAULT_MESSAGE_ERROR_TEMPLATE
     * @fieldType String
     */ 
    private static final String DEFAULT_MESSAGE_ERROR_TEMPLATE = "出现【%s】级别异常";
    
    /**  
     * @param request
     * @param exception
     * @return
     * @exception
     */ 
    @ExceptionHandler(value={WebException.class,ServiceException.class,DaoException.class,ControllerException.class})
    public BaseDTO<ApplicationException> catchPlatformException(HttpServletRequest request,Exception exception) {
        ApplicationException infoError = null;
        if(exception instanceof WebException) {
           infoError = new WebException(DEFAULT_MESSAGE_ERROR_TEMPLATE,exception);
        }else if(exception instanceof ControllerException) {
           infoError = new ControllerException(DEFAULT_MESSAGE_ERROR_TEMPLATE,exception);
        }else if(exception instanceof ServiceException) {
           infoError = new ServiceException(DEFAULT_MESSAGE_ERROR_TEMPLATE,exception);
        }else if(exception instanceof DaoException) {
           infoError = new DaoException(DEFAULT_MESSAGE_ERROR_TEMPLATE,exception);
        }
        log.error(String.format(DEFAULT_MESSAGE_ERROR_TEMPLATE,infoError.getLayerTypeEnum().getValue()),infoError);
        BaseDTO<ApplicationException> errorResult = new BaseDTO<>();
        errorResult.setCode(SystemConfiguration.ResultConfiguration.FAILURE.getCode());
        errorResult.setMessage(String.format(infoError.getMessage(),infoError.getLayerTypeEnum().getValue()));
        errorResult.setContent(infoError);
        return errorResult;
    }
    
    /**  
     * @param request
     * @param exception
     * @return
     * @exception
     */ 
    @ExceptionHandler(ApplicationException.class)
    public BaseDTO<ApplicationException> catchInfodbException(HttpServletRequest request,Exception exception) {
        ApplicationException infoError = null;
        if(exception instanceof ApplicationException) {
            infoError = new ApplicationException(exception);
        }
        log.error(String.format(DEFAULT_MESSAGE_ERROR_TEMPLATE,infoError.getLayerTypeEnum().getValue()),infoError);
        BaseDTO<ApplicationException> errorResult = new BaseDTO<>();
        errorResult.setCode(SystemConfiguration.ResultConfiguration.FAILURE.getCode());
        errorResult.setMessage(String.format(infoError.getMessage(),infoError.getLayerTypeEnum().getValue()));
        errorResult.setContent(infoError);
        return errorResult;
    }
    
    /**  
     * @param request
     * @param exception
     * @return
     * @exception
     */ 
    @ExceptionHandler(Exception.class)
    public BaseDTO<Exception> catchException(HttpServletRequest request,Exception exception) {
        log.error(String.format(DEFAULT_MESSAGE_ERROR_TEMPLATE,"框架级别"),exception);
        BaseDTO<Exception> errorResult = new BaseDTO<>();
        errorResult.setCode(SystemConfiguration.ResultConfiguration.FAILURE.getCode());
        errorResult.setMessage(exception.getMessage());
        errorResult.setContent(exception);
        return errorResult;
    } 
}
